<%--
  Created by IntelliJ IDEA.
  User: thankyouzero
  Date: 2018/6/9
  Time: 16:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>查询页面</title>
    <link href="css/style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<%
    //连接数据库
    String driver="com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/hbpw?useUnicode=true&characterEncoding=utf-8&useSSL=false";//地址
    String user="hbpwuser";
    String pass="LFFLXY";
    Class.forName(driver);                   //加载jdbc驱动
    Connection connection  = (Connection) DriverManager.getConnection(url,user,pass);

    Statement statement = (Statement) connection.createStatement();  //连接实例

    String sql="select  hd.HD_SXH AS 顺序号 , hd.HB_HBH AS 航班号,j1.JC_MC AS 出发机场,j2.JC_MC AS 到达机场 from hd,jc j1,jc j2 where j1.JC_DM=hd.JC_START AND j2.JC_DM=hd.JC_END;";
    ResultSet Rst = statement.executeQuery(sql);

%>

<script>
    //根据航班号选择出发-到达机场
    var onecount;
    onecount=0;
    subcat = new Array();//存储查询出的结果，即所有航班号，出发机场，到达机场，顺序号
    <%
        int count = 0;
        while(Rst.next()){
            %>
    subcat["<%=count%>"] = new Array('<%=Rst.getString("航班号")%>','<%=Rst.getString("出发机场")%>','<%=Rst.getString("到达机场")%>','<%=Rst.getString("顺序号")%>');
    <%
    count++;
}
Rst.close();
%>
    onecount="<%=count%>";
    //出发机场联动控制
    var hb=null;//存储此时的航班号
    function change(locationid){
        document.airport.JC_START.length = 0;
        var location_id=locationid;
        var length=0;
        var i;
        if(location_id=="0")
            document.airport.JC_START.options[0] = new Option("出发机场");
        else{
            for(i=0;i<onecount;i++){
                if (subcat[i][0] == location_id) {
                    document.airport.JC_START.options[length++] = new Option(subcat[i][1]);
                    hb=subcat[i][0];
                }
            }
        }
    }

    //到达机场联动控制
    function change1(locationid){
        document.airport.JC_END.length = 0;
        var location_id=locationid;
        var length=0;
        var i;
        if(location_id=="出发机场")
            document.airport.JC_END.options[0] = new Option("到达机场");
        else{
            var sxh;//当前出发机场所拥有的顺序号
            sxh=0;
            for(i=0;i<onecount;i++){
                if (subcat[i][1] == location_id&&hb==subcat[i][0]) {//某一航段出发机场就是上面的出发机场，直接加入
                    document.airport.JC_END.options[length++] = new Option(subcat[i][2]);
                    sxh=subcat[i][3];//并记录此时的顺序号（只有一个）
                }
                if(hb==subcat[i][0]&&sxh<subcat[i][3]&&sxh!=0)//输出顺序号大于当前出发机场所在航段顺序号的
                {
                    document.airport.JC_END.options[length++] = new Option(subcat[i][2]);
                }
            }
        }
    }
    //选择航班时改变到达机场
    function change2(locationid){
        document.airport.JC_END.length = 0;
        var location_id=locationid;
        var length=0;
        var i;
        if(location_id=="0")
            document.airport.JC_END.options[0] = new Option("到达机场");
        else{
            for(i=0;i<onecount;i++){
                if (subcat[i][0] == location_id) {
                    document.airport.JC_END.options[length++] = new Option(subcat[i][2]);
                }
            }
        }
    }
</script>
<form method="POST" name="airport" action="buypg3.jsp">
    <table>
        <tr><th colspan="8">用户购票信息</th></tr>
        <tr>
            <td >航班号</td>
            <td> <select name="typeId"  required = "required" onChange="change(document.airport.typeId.options[document.airport.typeId.selectedIndex].value),
            change2(document.airport.typeId.options[document.airport.typeId.selectedIndex].value);" >
                <option value="0">请选择</option>
                <%
                    String sql1 = "SELECT hb.HB_HBH  FROM hb;";
                    ResultSet rs1 = statement.executeQuery(sql1);
                    while(rs1.next()) {
                        String HB_HBH  = rs1.getString("HB_HBH");
                %>
                <option value="<%=HB_HBH%>"><%= HB_HBH %></option>;
                <%
                    }
                    rs1.close();
                %>
            </select></td>
        </tr>
        <tr>
            <td >出发机场</td>
            <td>
                <select name="JC_START"  required = "required" onChange="change1(document.airport.JC_START.options[document.airport.JC_START.selectedIndex].value);" >
                    <option value="0">出发机场</option>
                </select>
            </td>
        </tr>
        <tr>
            <td >到达机场</td>
            <td>
                <select name="JC_END"  required = "required">
                    <option value="0">到达机场</option>
                </select>
            </td>
        </tr>

        <tr>
            <td >日期</td>
            <td>
                <input name="date" required = "required" type="date">
            </td>
        </tr>
        <tr>
            <td colsp meiwan="8"><input type="submit" value="购票" />
            </td>
        </tr>
    </table>
</form>
</body>
</html>